import request from '../utils/request'

//...... 各个axios后台调用

//登录接口
export const getLogin = (info)=>{
    return request.post("user/login", info)
}

//通过token获取用户的信息
export const getUserInfo = (info)=>{
    return request.get("user/getUserInfo")
}


//订单页面
export const getOrdersByUserWithService = (uid) => {
    return request({
        method: 'get',
        url: 'orders/getOrdersByUserWithService',
        params: {uid}
    })
}

//删除订单
export const deleteOrderByid = (orderId)=> {
    return request({
        method: 'delete',
        url: 'orders/removeByOrderId',
         headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
         params: { orderId }
    })
}
//删除订单详情
export const deleteOrderDetailByid = (orderId)=> {
    return request({
        method: 'delete',
        url: 'orders/removeOrderDetailByOrderId',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        params: { orderId }
    })
}

//订单详情
export const getFullOrderDetail = (orderId) => {
    return request({
        method: 'get',
        url: 'orders/getFullOrderDetail',
        params: { orderId }
    })
}

//通过orderId获取订单
export const findOrderByOrderId = (orderId) => {
    return request({
        method: 'get',
        url: 'orders/findOrderByOrderId',
        params: {orderId}
    })
}


export const updateOrderDescription = (orderId, status, description) => {
  // 使用URLSearchParams构造表单数据
  const formData = new URLSearchParams();
  formData.append('orderId', orderId);
  formData.append('status', status); // 直接传递中文状态字符串
  formData.append('description', description);

  return request({
    method: 'post',
    url: 'orders/updateDescription',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    data: formData
  })
}

//更新地址
export const updateAddress = (address) => {
    return request.post("orders/updateAddress", address)
}

//更新订单详情
export const updateOrdetail = (orderDetail) => {
    return request.post("orders/updateOrderDetailByDetailId", orderDetail)
}

//案例
export const getShowNewsDetail = (hid)=>{
    return request({
        method: 'post',
        url: 'portal/showHeadlineDetail',
        data: `hid=${hid}`
    })
}


//delete 例子
export const deleteNewsByHid = (hid)=>{
    return request({
        method: 'delete',
        url: 'headline/removeByHid',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: `hid=${hid}`
    })
}
// 检查用户名是否存在
export const checkUserName = (username)=>{
    return request({
        method: 'post',
        url: 'user/checkUserName',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: `username=${username}`
    })
}
// 检查登录状态（Token有效性）
export const checkLogin = (userData)=>{
    return request.get("user/checkLogin",userData)
}


//获取服务分类列表
export const getFindAllTypes = ()=>{
  return request.get("portal/findAllTypes")
}
//获取侧边导航栏分类
// export const getFindSideTypes = (params)=>{
//   return request.get("portal/selectSidebar",{ params })
// }
export const getFindSideTypes = (categoryName) => {
  return request.get('portal/selectSidebar', {
    params: {
      categoryName: categoryName // 参数名必须与后端@RequestParam一致
    }
  })
}

//查询服务
export const getServiceByName = (params) => {
  return request({
    url: 'portal/findServiceByName',
    method: 'get',
    params: params  // 直接传递参数对象
  })
}

//获取具体服务分类的列表
export const getFindServices = (sidebarName)=>{
    return request.get("portal/findService",{
        params:{
            sidebarName:sidebarName
        }
    })
}
//图片上传
export const uploadImageApi = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  
  return request.post('/api/upload/uploadImage', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};
//获取服务
export const getServiceById = (serviceId) => {
    return request.post("service/getServiceById", null, {
        params: { serviceId } // 作为 URL 查询参数
        // 或
        // data: { serviceId } // 作为表单数据（需配置 axios 的 headers）
    });   
};

export const registerid = (userData)=>{
    return request.post('user/register',userData)
}

//查询默认地址
export const getaddressById = (userId) => {
    return request.post("service/getaddressById", null, {
        params: { userId } // 作为 URL 查询参数
    
    });   
};
//查询非默认地址
export const getNotaddressById = (userId) => {
    return request.post("service/getnotaddressById", null, {
        params: { userId } // 作为 URL 查询参数
    
    });   
};


//保存地址
export const getaddAddress = (address) => {
    return request.post("service/addAddress", address)
}
//获取分类
export const getCategoryById = (categoryId) => {
    return request.post("service/getCategoryById", null, {
        params: { categoryId } // 作为 URL 查询参数
       
    });   
};
//通过addressId获取地址
export const getAddressByAddressId = (addressId) => {
    return request({
        method: 'get',
        url: 'service/getAddressByAddressId',
        params: {addressId}
    })
}
//创建订单和保存详情
export const getOrderDetails = (orderDetails) => {
    return request.post("service/addOrders", orderDetails)
}

export const getAllRecommendations=()=>{
    return request.get("recommendations/getAll")
}

export const getService=()=>{
    return request.get("service/getServiceByPopular")
}

//获取订单详情通过orderId
export const getDetailsByOrderId = (orderId) => {
    return request({
        method: 'get',
        url: 'orders/getDetailsByOrderId',
        params: {orderId}
    })
}
